﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataAccessComponents;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using SmsApi;
using System.Threading;
using BusinessLogicLayer;

namespace inzDesktop
{
    /// <summary>
    /// Interaction logic for windowSms.xaml
    /// </summary>
    public partial class windowSms : Window
    {
        DaneFirma daneFirma;
        public windowSms()
        {
            InitializeComponent();
            
        }

        public void inicjalizujZawartosc(ObservableCollection<Klient> listaKlientow)
        {
            //dane firmy
            DaneFirmaBL danefirmaBL = new DaneFirmaBL();
            daneFirma = danefirmaBL.pobierz("");

            dataGridDo.ItemsSource = listaKlientow;
            lblDo.Content = "Do:\n(" + listaKlientow.Count + ")";
            
            akutalizujStanPunktow();


        }

        private void btnAnuluj_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void akutalizujStanPunktow()
        {
            SmsMessage sms = new SmsMessage();
            SmsClient sc = new SmsClient(daneFirma.Login_sms, daneFirma.Haslo_sms, daneFirma.Host_sms);

            sms.Points = 1;
            sms.Details = 1;

           

            SmsResponse sr = sc.amountOfPoints(sms);

            lblIloscPunktow.Content = sr.Pkt;
            lblIloscEco.Content = sr.Eco;
            lblIloscPro.Content = sr.Pro;
        }

        private void btnWyslij_Click(object sender, RoutedEventArgs e)
        {
            SmsMessage sms = new SmsMessage();
            SmsClient sc = new SmsClient(daneFirma.Login_sms,daneFirma.Haslo_sms,daneFirma.Host_sms);

            // Dodawanie adresatow
            foreach (Klient klient in dataGridDo.ItemsSource)
            {
                sms.To.Add(new SmsAddress(klient.Tel_komorkowy));
            }            

            // Wiadomosc
            sms.Message = tbWiadomosc.Text;
            sms.Test = 0;
            sms.Details = 1;

            // Wybranie trybu eco
            if(rbEco.IsChecked == true)
                sms.Eco = 1;            
            

            tBWynik.Text = "";
            borWynik.BorderThickness = new Thickness(0);
            busyIdicator.IsBusy = true;

            List<SmsResponse> lista = sc.send(sms);
            if (lista != null)
            {
                if (lista[0].Status.Contains("OK"))
                {
                    tBWynik.Text = String.Format("Status: {0}. Wysłano {1} wiadomości.", lista[0].Status, lista[0].Smscount);
                    tBWynik.Foreground = Brushes.Green;
                    borWynik.BorderBrush = Brushes.Green;
                    borWynik.BorderThickness = new Thickness(2);
                }
                else
                {
                    tBWynik.Text = String.Format("Status: {0}.\nBłąd nr:{1} : {2}", lista[0].Status, lista[0].Err, SmsResponse.errorDescription(lista[0].Err));
                    tBWynik.Foreground = Brushes.Red;
                    borWynik.BorderBrush = Brushes.Red;
                    borWynik.BorderThickness = new Thickness(2);
                }
                akutalizujStanPunktow();
                busyIdicator.IsBusy = false;
            }
            else
            {
                busyIdicator.IsBusy = false;
                MessageBox.Show("Niepoprawny lub brak adresu usługi", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
            }
                  

        }

        private void btnAnulujWysylanie_Click(object sender, RoutedEventArgs e)
        {

        }

        private void tbWiadomosc_TextChanged(object sender, TextChangedEventArgs e)
        {
            int ilosc_znakow = tbWiadomosc.Text.Length;
            
            int standard = 160;
            int specjal = 70;

            string pattern = @"[@£_^{}\[~\]|ÆæßÉ!¤%&'()\*\+,-.:;<=>ÄÖÑÜ§¿äöñüànąężźćłóś]+";
           // string pattern = @"[\@\#\$\%\^\&\*\(\)ąężźćłóś]+";
            
            //Match match = Regex.Match(tbWiadomosc.Text,pattern);
            if (Regex.IsMatch(tbWiadomosc.Text, pattern))
            {
                lblZnaki.Content = String.Format("Liczba wiadomości:{0} Ilość znaków:{1}", Math.Ceiling((float)ilosc_znakow / specjal), ilosc_znakow);
                tbWiadomosc.MaxLength = 201;

            }
            else
            {
                lblZnaki.Content = String.Format("Liczba wiadomości:{0} Ilość znaków:{1}", Math.Ceiling((float)ilosc_znakow / standard), ilosc_znakow);
                tbWiadomosc.MaxLength = 459;
            }
        
            
        }
    }
}
